//
//  MMTextStyleViewController.m
//  MemeMaker
//
//  Created by dangthan on 7/13/13.
//  Copyright (c) 2013 soyo. All rights reserved.
//

#import "MMTextStyleViewController.h"
#import "MMAlertInputTextView.h"
#import <QuartzCore/QuartzCore.h>
#import "MMTextStyleModel.h"

#define kPaintingViewTag 11
#define kRectTextPreview CGRectMake(0.0, 10.0, 300.0, 50.0)
@interface MMTextStyleViewController () {
    CGRect currentRect;
}
@property (weak, nonatomic) IBOutlet UIButton *btnCancel;
@property (weak, nonatomic) IBOutlet UIButton *btnApply;
@end

@implementation MMTextStyleViewController
@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSInteger minus = 0;
    if (!SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        minus = -STATUS_BAR_HEIGHT;
    }
    
    CGRect frame1 = self.btnCancel.frame;
    frame1.origin.y += minus;
    self.btnCancel.frame = frame1;
    
    CGRect frame2 = self.btnApply.frame;
    frame2.origin.y += minus;
    self.btnApply.frame = frame2;
    
    //init text properties
    currentFont = [UIFont boldSystemFontOfSize:30.0];
    currentTextSize = 30;
    currentColor = [UIColor blackColor];
    currentTextAlignment = NSTextAlignmentCenter;
    
    paintingView = [[MMPaintingView alloc] initWithFrame:CGRectMake(10, 65+minus, 300, 58)];
    paintingView.layer.borderColor = [UIColor darkTextColor].CGColor;
    paintingView.layer.borderWidth = 1.0;
    paintingView.tag = kPaintingViewTag;
    [paintingView setUserInteractionEnabled:NO];
    [self.view addSubview:paintingView];
    txtInput = @"Sample";
    [paintingView drawText:txtInput atRect:kRectTextPreview withFont:currentFont color:currentColor andTextAlignment:currentTextAlignment];
    currentRect = kRectTextPreview;
    btnInput.hidden = YES;
    
    MMTextStyleView *textStyle = [MMUtils loadView:[MMTextStyleView class] FromNib:@"MMTextStyleView"];
    [textStyle setFrame:CGRectMake(10, 128+minus, 300, 141)];
    [textStyle setDelegate:self];
    [self.view addSubview:textStyle];
    MMColorBoard *colorBoar = [MMUtils loadView:[MMColorBoard class] FromNib:@"MMColorBoard"];
    [colorBoar setDelegate:self];
    [colorBoar setFrame:CGRectMake(10,
                                   textStyle.frame.origin.y + textStyle.frame.size.height + 5.0,
                                   300,
                                   112.0)];
    [self.view addSubview:colorBoar];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    btnInput = nil;
    [super viewDidUnload];
}


#pragma mark - MMTextStyle Delegate
- (void) didChangedAlignment:(NSTextAlignment)align {
    currentTextAlignment = align;
    [paintingView clear];
    [paintingView drawText:txtInput atRect:currentRect withFont:currentFont color:currentColor andTextAlignment:currentTextAlignment];
}


- (void) didChangedSize:(CGFloat)textSize andFontType:(FONTTYPE)type {
    if (type == REGULAR) {
        currentFont = [UIFont systemFontOfSize:textSize];
    } else if (type == BOLD) {
        currentFont = [UIFont boldSystemFontOfSize:textSize];
    } else {
        currentFont = [UIFont italicSystemFontOfSize:textSize];
    }
    
    if (currentTextSize != textSize) {
        if (currentTextSize < textSize) {
            currentRect.origin.y -=  (textSize - currentTextSize)/2;
        } else if (currentTextSize > textSize) {
            currentRect.origin.y +=  (currentTextSize - textSize)/2;
        }
        currentTextSize = textSize;
    }
    
    [paintingView clear];
    [paintingView drawText:txtInput atRect:currentRect withFont:currentFont color:currentColor andTextAlignment:currentTextAlignment];
}


#pragma mark - MMColorBar Delegate
- (void) didSelectColor:(UIColor *)color {
    currentColor = color;
    [paintingView clear];
    [paintingView drawText:txtInput atRect:currentRect withFont:currentFont color:currentColor andTextAlignment:currentTextAlignment];
}

#pragma mark - MMColorBoard Delegate
- (void) didSeletedColorBoard:(UIColor *)color {
    currentColor = color;
    [paintingView clear];
    [paintingView drawText:txtInput atRect:currentRect withFont:currentFont color:currentColor andTextAlignment:currentTextAlignment];
}

- (void) showBarColor {
    if (!colorBar) {
        NSInteger minus = 0;
        if (!SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
            minus = -STATUS_BAR_HEIGHT;
        }
        colorBar = [[MMColorBar alloc] initWithFrame:CGRectMake(10.0,
                                                                391.0+minus,
                                                                300.0,
                                                                83.0)];
        [colorBar setDelegate:self];
        [self.view addSubview:colorBar];
    }
    [colorBar setHidden:NO];
}

- (void) hideBarColor {
    [colorBar setHidden:YES];
}

- (IBAction)applyOrCancel:(id)sender {
    UIButton *btn = (UIButton*) sender;
    
    if (btn.tag == 1) {
        //do nothing
    } else if (btn.tag == 2) {
        MMTextStyleModel *textStyleModel = [[MMTextStyleModel alloc] init];
        [textStyleModel setPTextAlignment:currentTextAlignment];
        [textStyleModel setPColor:currentColor];
        [textStyleModel setPFont:currentFont];
        if (delegate && [delegate respondsToSelector:@selector(didFinishedChanged:)]) {
            [delegate didFinishedChanged:textStyleModel];
        }
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)showTextField:(id)sender {
    MMAlertInputTextView *alrInput = [[MMAlertInputTextView alloc] initWithTitle:@"Text to input" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK"];
    alrInput.tag = 100;
    [alrInput show];
}


#pragma mark - UIAlertView Delegate

- (void) didPresentAlertView:(UIAlertView *)alertView {
    if ([alertView isKindOfClass:[MMAlertInputTextView class]]) {
        MMAlertInputTextView *alertPattern = (MMAlertInputTextView *)alertView;
        alertPattern.textInput.text = txtInput; 
        [alertPattern.textInput becomeFirstResponder];
        [alertPattern setNeedsLayout];
    }
}

- (void) alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
    if ([alertView isKindOfClass:[MMAlertInputTextView class]]) {
        if (buttonIndex == 0) {
            return;
        } else {
            MMAlertInputTextView *alertText = (MMAlertInputTextView *) alertView;
            if ([alertText.textInput.text isEqualToString:@""]) {
                //do nothing
            } else {
                txtInput = alertText.textInput.text;
            }
        }
    }
    [paintingView clear];
    [paintingView drawText:txtInput atRect:currentRect withFont:currentFont color:currentColor andTextAlignment:currentTextAlignment];
}

- (void) didChangeWithImage:(UIImage *)img rect:(CGRect)rect isNotification:(BOOL)isNotify{
    
}


@end
